package com.shisfish.library.Controller;

import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import net.sf.json.JSONArray;

import com.shisfish.library.Impl.AdminImpl;
import com.shisfish.library.Impl.BookImpl;
import com.shisfish.library.Impl.BookLoanImpl;
import com.shisfish.library.Impl.BookSingleImpl;
import com.shisfish.library.Impl.InterestImpl;
import com.shisfish.library.Impl.RecordImpl;
import com.shisfish.library.Impl.UserImpl;
import com.shisfish.library.Impl.UserInterestImpl;
import com.shisfish.library.Model.Admin;
import com.shisfish.library.Model.Interest;
import com.shisfish.library.Model.Record;
import com.shisfish.library.Model.User;
import com.shisfish.library.Model.UserInterest;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.CacheUtils;
import com.shisfish.library.Utils.DateFormatUtils;
import com.shisfish.library.Utils.EncryptUtils;
import com.shisfish.library.Utils.JsonValueUtil;
import com.shisfish.library.Utils.LoginUtils;
import com.shisfish.library.Utils.RandomUtils;
import com.shisfish.library.Utils.FileUpload.FileUploadUtils;
import com.shisfish.library.Utils.FileUpload.FormJsonArrUtil;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;
import com.shisfish.library.Utils.PageModel.Page;
import com.shisfish.library.Utils.PageModel.PageUtils;

@Path("admin")
public class AdminController extends GlobalVariable {

	private AdminImpl adminImpl = new AdminImpl();
	
	private UserImpl userImpl = new UserImpl();

	FileUploadUtils fileUploadUtils = new FileUploadUtils();

	JsonValueUtil jsonValueUtil = new JsonValueUtil();
	
	private RecordImpl recordImpl = new RecordImpl();
	
	private InterestImpl interestImpl = new InterestImpl();
	
	private UserInterestImpl userInterestImpl = new UserInterestImpl();
	

	/**
	 * @Author shisfish
	 * @Date 2015年12月31日 下午4:29:05
	 * @Descrition : 新增管理员
	 * @param idadmin
	 * @param request
	 * @return
	 */
	@POST
	@Path("insertAAdmin/{idadmin}")
	@Consumes({ MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_JSON })
	@Produces(MediaType.TEXT_PLAIN)
	public String insertAAdmin(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request) {
		
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			// 通过 idadmin 查找权限 只有role = 0才能添加用户
			
			List<Admin> admins = adminImpl.selectByKey(idadmin);
			
			if (admins.size() > 0) {
				if (admins.get(0).getRole() == 0) {
					// 取得所有文件和表单值
					JSONArray jsArr = fileUploadUtils.getRiverWayUpload(request);
					// 取得所有普通表单值
					JSONArray customjsArr = new FormJsonArrUtil().getFormJsonArr(jsArr, "form");
					// 获取form表单数据
					String userId = jsonValueUtil.getValue(customjsArr, "userId");
					String username = jsonValueUtil.getValue(customjsArr, "username");
					String pass = jsonValueUtil.getValue(customjsArr, "password");

					String password = pass;

					String ran = RandomUtils.getRandomString();

					try {
						password = EncryptUtils.getMD5(pass + ran);
					} catch (NoSuchAlgorithmException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					int role = Integer
							.parseInt(jsonValueUtil.getValue(customjsArr, "role"));

					String adminid = "admin" + DateFormatUtils.getDateformat_1(new Date());
					Admin admin = new Admin(adminid, username, password, role, 0, ran, userId);

					int flag = adminImpl.insertOne(admin);

					if (flag == 0) {
						globalMap.put(STATUS_CODE, INSERT_WR);
					} else {
						globalMap.put(STATUS_CODE, SUCCESS);
					}
				}
				else {
					globalMap.put(STATUS_CODE, INADEQUATE_PERMISSIONS);
				}
			}
			
		}
		else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		return toJsonUtils.map2Json(globalMap);
	}

	
	/**
	 * @Author shisfish
	 * @Date 2015年12月31日 下午4:29:38
	 * @Descrition : 管理员登录
	 * @param userId
	 * @param password
	 * @param request
	 * @return
	 */
	@POST
	@Path("login")
	@Produces("application/json; charset=utf-8")
	public String login(@FormParam("userId") String userId,
			@FormParam("password") String password, 
			@Context HttpServletRequest request) {

		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<Admin> admins = adminImpl.selectByUserId(userId);
		
		//List<Admin> admins = adminImpl.selectByKey(idadmin);
		
		if (admins.size() > 0) {
			Admin admin = admins.get(0);
			String pass = password;
			try {
				pass = EncryptUtils.getMD5(password + admin.getRan());

				if (admin.getPassword().equals(pass)) {
					admin.setRecord(1);
					int flag = adminImpl.updateOne(admin);
					if (flag == 0) {
						globalMap.put(STATUS_CODE, SELECT_WR);
					} else {
						globalMap.put(STATUS_CODE, SUCCESS);
						
						Cache cache = CacheUtils.getCache(ADMIN_LOGIN, request);
						
						if (cache != null) {
							cache.put(new Element("idadmin", admin.getIdadmin()));
							cache.put(new Element(ISLOGIN, "1"));
						}
						
						
					
						Calendar cal = Calendar.getInstance();
						cal.add(Calendar.DAY_OF_YEAR, 30);
						
						recordImpl.deleteByIDT(admin.getIdadmin(), 1);
						
						Record record = new Record(admin.getIdadmin(), cal.getTime(), 1);
						int flag_1 = recordImpl.insertOne(record);
						if (flag_1 == 0) {
							globalMap.put(STATUS_CODE, INSERT_WR);
						} else {
							List<Map<String, Object>> map = adminImpl.selectUserId(userId);
							globalMap.put("admin", map.get(0));
						}
						
					}
				}
				else {
					globalMap.put(STATUS_CODE, LOGIN_WR);
				}

			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} else {
			globalMap.put(STATUS_CODE, LOGIN_WR);
		}

		return toJsonUtils.map2Json(globalMap);
	}

	/**
	 * @Author shisfish
	 * @Date 2015年12月31日 下午4:29:48
	 * @Descrition : 管理员修改密码
	 * @param userid
	 * @param passwordOld
	 * @param passwordNew
	 * @param request
	 * @return
	 */
	@POST
	@Path("changePass/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String changePass(@PathParam("idadmin") String idadmin,
			@FormParam("passwordOld") String passwordOld,
			@FormParam("passwordNew") String passwordNew, 
			@Context HttpServletRequest request) {

		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (LoginUtils.isLoginAdmin(idadmin, request)) {
			String passOld = passwordOld;
			try {

				List<Admin> admins = adminImpl.selectByKey(idadmin);

				if (admins.size() > 0) {

					Admin admin = admins.get(0);
					
					passOld = EncryptUtils
							.getMD5(passwordOld + admin.getRan());

					// 才能进行改密码
					if (admin.getPassword().equals(passOld)) {

						String passNew = EncryptUtils.getMD5(passwordNew
								+ admin.getRan());

						admin.setPassword(passNew);

						// 改了密码之后， 把登陆状态取消
						admin.setRecord(0);

						int flag = adminImpl.updateOne(admin);

						if (flag == 0) {
							globalMap.put(STATUS_CODE, CHANGE_PASS_WR);
						} else {
							globalMap.put(STATUS_CODE, SUCCESS);
						}

					} else {
						globalMap.put(STATUS_CODE, PASS_WR);
					}
				}

			} catch (NoSuchAlgorithmException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}

		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @Author shisfish
	 * @Date 2015年12月31日 下午4:29:59
	 * @Descrition : 新增一个管理员
	 * @param idadmin
	 * @param request
	 * @return
	 */
	@POST
	@Path("insertAUser/{idadmin}")
	@Consumes({ MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_JSON })
	@Produces(MediaType.TEXT_PLAIN)
	public String insertAUser(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request) {

		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			// 取得所有文件和表单值
			JSONArray jsArr = fileUploadUtils.getRiverWayUpload(request);
			// 取得所有普通表单值
			JSONArray customjsArr = new FormJsonArrUtil().getFormJsonArr(jsArr, "form");
			// 获取form表单数据
			String userId = jsonValueUtil.getValue(customjsArr, "userId");
			String username = jsonValueUtil.getValue(customjsArr, "username");
			String util = jsonValueUtil.getValue(customjsArr, "util");
			String department = jsonValueUtil.getValue(customjsArr, "department");
			String company = jsonValueUtil.getValue(customjsArr, "company");
			String address = jsonValueUtil.getValue(customjsArr, "address");
			String qq = jsonValueUtil.getValue(customjsArr, "qq");
			String wechat = jsonValueUtil.getValue(customjsArr, "wechat");
			String email = jsonValueUtil.getValue(customjsArr, "email");
			String mobile_phone = jsonValueUtil.getValue(customjsArr,
					"mobile_phone");
			String phone = jsonValueUtil.getValue(customjsArr, "phone");
			String sexstr = jsonValueUtil.getValue(customjsArr, "sex");
			int sex = 0;
			if (!sexstr.isEmpty()) {
				sex = Integer.parseInt(sexstr);
			}
			int age = 0;
			String agestr = jsonValueUtil.getValue(customjsArr, "age");
			if (!agestr.isEmpty()) {
				age = Integer.parseInt(agestr);
			}
			String birthday = jsonValueUtil.getValue(customjsArr, "birthday");
			String loanid = jsonValueUtil.getValue(customjsArr, "loanid");;
			String password = jsonValueUtil.getValue(customjsArr, "password");;
			String ran = RandomUtils.getRandomString();
			int level = 0;
			String levelstr = jsonValueUtil.getValue(customjsArr, "level");
			if (!levelstr.isEmpty()) {
				level = Integer.parseInt(levelstr);
			}
			
			try {
				String pass = EncryptUtils.getMD5(password + ran);
				
				String employee = "EM" + DateFormatUtils.getDateformat_1(new Date());
				
				User user = new User(employee, username, department, util, company,
						address, qq, wechat, email, mobile_phone, phone, sex, age,
						birthday, loanid, pass, ran, level, 0, userId);

				int flag = userImpl.insertOne(user);

				if (flag == 0) {
					globalMap.put(STATUS_CODE, INSERT_WR);
				} else {
					globalMap.put(STATUS_CODE, SUCCESS);
					
					// 添加用户兴趣
					List<Interest> interests = interestImpl.selectType(1);
					String inte = "";
					if (interests.size() > 0) {
						StringBuffer sb = new StringBuffer();
						
						for (Interest interest : interests) {
							sb.append(interest.getId());
							sb.append(",");
						}
						String s = sb.toString();
						s = s.substring(0, s.length() - 1);
						inte += s;
										
					}
					
					UserInterest userInterest = new UserInterest(employee, inte, "", "");
					
					userInterestImpl.insertOne(userInterest);
					
					
					
				}
				
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @Author shisfish
	 * @Date 2015年12月31日 下午4:30:20
	 * @Descrition : 查询所有的管理员（分页）
	 * @param idadmin
	 * @param currentPage
	 * @param pageSize
	 * @param request
	 * @return
	 */
	@GET
	@Path("selectAllAdmin/{idadmin}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectAllAdmin(@PathParam("idadmin") String idadmin, @PathParam("currentPage") int currentPage, 
			@PathParam("pageSize") int pageSize, @Context HttpServletRequest request) {
		
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			int count = adminImpl.countAll();
			
			if (count > 0) {
				Page page = PageUtils.createPage(count, currentPage, pageSize);
				
				List<Admin> admins = adminImpl.selectAllPage(page.getBeginIndex(), pageSize);
				
				if (admins.size() > 0) {
					for (Admin admin : admins) {
						admin.setPassword("");
						admin.setRan("");
					}
					globalMap.put("list", admins);
					globalMap.put("page", page);
					globalMap.put(STATUS_CODE, SUCCESS);
				}
				
				
			} 
			else if (count == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
			else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}

		}
		else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @Author shisfish
	 * @Date 2015年12月31日 下午4:30:42
	 * @Descrition : 查询所有的管理员
	 * @param idadmin
	 * @param request
	 * @return
	 */
	@GET
	@Path("selectAll/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String selectAll(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request) {
		
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			List<Admin> admins = adminImpl.selectAll();
			
			if (admins.size() > 0) {
				for (Admin admin : admins) {
					admin.setPassword("");
					admin.setRan("");
				}
				globalMap.put("list", admins);
				globalMap.put(STATUS_CODE, SUCCESS);
			}
			else if (admins.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
			else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}

		}
		else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @Author shisfish
	 * @Date 2015年12月31日 下午4:30:59
	 * @Descrition : 查询所有的用户（分页）
	 * @param idadmin
	 * @param currentPage
	 * @param pageSize
	 * @param request
	 * @return
	 */
	@GET
	@Path("selectAllUser/{idadmin}/{type}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectAllUser(@PathParam("idadmin") String idadmin, @PathParam("currentPage") int currentPage, 
			@PathParam("pageSize") int pageSize, @Context HttpServletRequest request, @PathParam("type") int type) {
		
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			int count = userImpl.countType(type);
			
			if (count > 0) {
				Page page = PageUtils.createPage(count, currentPage, pageSize);
				
				List<User> users = userImpl.selectTypePage(type, page.getBeginIndex(), pageSize);
				
				if (users.size() > 0) {
					for (User user : users) {
						user.setPassword("");
						user.setRan("");
					}
					globalMap.put("list", users);
					globalMap.put("page", page);
					globalMap.put(STATUS_CODE, SUCCESS);
				}
				
				
			} 
			else if (count == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
			else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}

		}
		else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @Author shisfish
	 * @Date 2015年12月31日 下午4:31:15
	 * @Descrition : 通过用户id注销一个用户
	 * @param idadmin
	 * @param employee
	 * @param request
	 * @return
	 */
	@GET
	@Path("cancelAUser/{idadmin}/{employee}")
	@Produces("application/json; charset=utf-8")
	public String cancelAUser(@PathParam("idadmin") String idadmin, @PathParam("employee") String employee, 
			@Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			List<Admin> admins = adminImpl.selectByKey(idadmin);
			
			if (admins.size() > 0 && admins.get(0).getRole() == 0) {
				List<User> users = userImpl.selectByEmployee(employee);
				
				if (users.size() > 0) {
					User user = users.get(0);
					user.setDeleteStatus(1);
					int flag = userImpl.updateOne(user);
					if (flag == 1) {
						globalMap.put(STATUS_CODE, SUCCESS);
					} else {
						globalMap.put(STATUS_CODE, UPDATE_WR);
					}
				} 
				else if (users.size() == 0) {
					globalMap.put(STATUS_CODE, SELECT_EMPTY);
				}
				else {
					globalMap.put(STATUS_CODE, SELECT_WR);
				}
			} else {
				globalMap.put(STATUS_CODE, INADEQUATE_PERMISSIONS);
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("resotreAUser/{idadmin}/{employee}")
	@Produces("application/json; charset=utf-8")
	public String resotreAUser(@PathParam("idadmin") String idadmin, @PathParam("employee") String employee, 
			@Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			List<Admin> admins = adminImpl.selectByKey(idadmin);
			
			if (admins.size() > 0 && admins.get(0).getRole() == 0) {
				List<User> users = userImpl.selectByEmployee1(employee);
				
				if (users.size() > 0) {
					User user = users.get(0);
					user.setDeleteStatus(0);
					int flag = userImpl.updateOne(user);
					if (flag == 1) {
						globalMap.put(STATUS_CODE, SUCCESS);
					} else {
						globalMap.put(STATUS_CODE, UPDATE_WR);
					}
				} 
				else if (users.size() == 0) {
					globalMap.put(STATUS_CODE, SELECT_EMPTY);
				}
				else {
					globalMap.put(STATUS_CODE, SELECT_WR);
				}
			} else {
				globalMap.put(STATUS_CODE, INADEQUATE_PERMISSIONS);
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @Author shisfish
	 * @Date 2015年12月31日 下午4:31:36
	 * @Descrition : 通过管理员id注销一个管理员
	 * @param idadmin
	 * @param userId
	 * @param request
	 * @return
	 */
	@GET
	@Path("cancelAAdmin/{idadmin}/{userId}")
	@Produces("application/json; charset=utf-8")
	public String cancelAAdmin(@PathParam("idadmin") String idadmin, @PathParam("userId") String userId, 
			@Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			List<Admin> admins = adminImpl.selectByKey(idadmin);
			
			if (admins.size() > 0 && admins.get(0).getRole() == 0) {
				List<Admin> adminS = adminImpl.selectByKey(userId);
				
				if (adminS.size() > 0) {
					Admin admiN = adminS.get(0);
					admiN.setDeleteStatus(1);
					int flag = adminImpl.updateOne(admiN);
					if (flag == 1) {
						globalMap.put(STATUS_CODE, SUCCESS);
					} else {
						globalMap.put(STATUS_CODE, UPDATE_WR);
					}
				} 
				else if (adminS.size() == 0) {
					globalMap.put(STATUS_CODE, SELECT_EMPTY);
				}
				else {
					globalMap.put(STATUS_CODE, SELECT_WR);
				}
			} else {
				globalMap.put(STATUS_CODE, INADEQUATE_PERMISSIONS);
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}

	/**
	 * @Author shisfish
	 * @Date 2015年12月31日 下午4:36:32
	 * @Descrition : 注销自己的管理员账户
	 * @param idadmin
	 * @param request
	 * @return
	 */
	@GET
	@Path("cancelAdmin/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String cancelAdmin(@PathParam("idadmin") String idadmin,  
			@Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			List<Admin> admins = adminImpl.selectByKey(idadmin);
			
			if (admins.size() > 0 ) {
				Admin admin = admins.get(0);
				admin.setDeleteStatus(1);
				int flag = adminImpl.updateOne(admin);
				
				if (flag == 1) {
					globalMap.put(STATUS_CODE, SUCCESS);
				} else {
					globalMap.put(STATUS_CODE, UPDATE_WR);
				}
				
			} else {
				globalMap.put(STATUS_CODE, UPDATE_WR);
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @author shisfish
	 * @date 2016年2月5日 下午11:02:55
	 * @Description 退出登录
	 * @param idadmin
	 * @param request
	 * @return
	 */
	@GET
	@Path("logout/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String logout(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			Admin admin = adminImpl.selectByID(idadmin);
			
			if (admin != null) {
				admin.setRecord(0);
				int flag = adminImpl.updateOne(admin);
				if (flag == 1) {
					globalMap.put(STATUS_CODE, SUCCESS);
				} else {
					globalMap.put(STATUS_CODE, UPDATE_WR);
				}
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("resetPassword/{idadmin}/{type}/{userid}")
	@Produces("application/json; charset=utf-8")
	public String resetPassword(@PathParam("type") int type, @PathParam("userid") String userid, 
			@Context HttpServletRequest request, @PathParam("idadmin") String idadmin) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			if (type == 1) {
				
				List<Admin> admins = adminImpl.selectByKey(userid);
				if (admins.size() > 0) {
					Admin admin = admins.get(0);
					String pass = "123456" + admin.getRan();
					try {
						String pa = EncryptUtils.getMD5(pass);
						admin.setPassword(pa);
						int flag = adminImpl.changePassword(userid, pa);
						if (flag == 0) {
							globalMap.put(STATUS_CODE, UPDATE_WR);
						} else {
							globalMap.put(STATUS_CODE, SUCCESS);
						}
					} catch (NoSuchAlgorithmException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				} else if (admins.size() == 0) {
					globalMap.put(STATUS_CODE, SELECT_EMPTY);
				} else {
					globalMap.put(STATUS_CODE, SELECT_WR);
				}
				
			} else if (type == 2) {
				List<User> users = userImpl.selectByEmployee(userid);
				if (users.size() > 0) {
					User user = users.get(0);
					String pass = "123456" + user.getRan();
					try {
						String pa = EncryptUtils.getMD5(pass);
						user.setPassword(pa);
						int flag = userImpl.changePassword(userid, pa);
						if (flag == 0) {
							globalMap.put(STATUS_CODE, UPDATE_WR);
						} else {
							globalMap.put(STATUS_CODE, SUCCESS);
						}
					} catch (NoSuchAlgorithmException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				} else if (users.size() == 0) {
					globalMap.put(STATUS_CODE, SELECT_EMPTY);
				} else {
					globalMap.put(STATUS_CODE, SELECT_WR);
				}
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("allMessage")
	@Produces("application/json; charset=utf-8")
	public String allMessage() {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		// 总共有几类书
		int book_count = new BookImpl().countAll();
		
		// 总共几类期刊
		int journal_count = new BookImpl().countAllJ();
		
		//总共几本书
		int bookSingle_count1 = new BookSingleImpl().countByTypeState(1, 3);
		int bookSingle_count2 = new BookSingleImpl().countByTypeState(2, 3);
		
		// 总共几本书被借
		int lend_count1 = new BookSingleImpl().countByTypeState(1, 0);
		int lend_count2 = new BookSingleImpl().countByTypeState(1, 2);
		
		// 总共几本未被借
		int notlend_count1 = new BookSingleImpl().countByTypeState(1, 1);
		
		// 总共几本期刊被借
		int lend_count3 = new BookSingleImpl().countByTypeState(2, 0);
		int lend_count4 = new BookSingleImpl().countByTypeState(2, 2);
		
		// 总共几本期刊未被借
		int notlend_count2 = new BookSingleImpl().countByTypeState(2, 1);
		
		// 几本被预约
		int loan_count1 = new BookLoanImpl().countBook(1);
		
		// 基本期刊被预约
		int loan_count2 = new BookLoanImpl().countBook(2);
		
		// 书类
		globalMap.put("book_count", book_count);
		// 期刊类
		globalMap.put("journal_count", journal_count);
		// 单本书
		globalMap.put("bookSingle_count", bookSingle_count1);
		// 单本期刊
		globalMap.put("journalSingle_count", bookSingle_count2);
		// 书本出借
		globalMap.put("book_lend_count", lend_count1 + lend_count2);
		// 期刊出借
		globalMap.put("journal_lend_count", lend_count3 + lend_count4);
		// 书本未出借
		globalMap.put("book_notlend_count", notlend_count1);
		// 期刊未出借
		globalMap.put("journal_notlend_count", notlend_count2);
		// 书本预约
		globalMap.put("book_loan_count", loan_count1);
		// 期刊预约
		globalMap.put("journal_loan_count", loan_count2);
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("updateAUser/{idadmin}/{employee}")
	@Consumes({ MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_JSON })
	@Produces(MediaType.TEXT_PLAIN)
	public String updateAUser(@PathParam("idadmin") String idadmin, 
			@PathParam("employee") String employee, @Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			
			// 取得所有文件和表单值
			JSONArray jsArr = fileUploadUtils.getRiverWayUpload(request);
			// 取得所有普通表单值
			JSONArray customjsArr = new FormJsonArrUtil().getFormJsonArr(jsArr, "form");
			// 获取form表单数据
			//String employee = jsonValueUtil.getValue(customjsArr, "employee");
			String username = jsonValueUtil.getValue(customjsArr, "username");
			String util = jsonValueUtil.getValue(customjsArr, "util");
			String department = jsonValueUtil.getValue(customjsArr, "department");
			String company = jsonValueUtil.getValue(customjsArr, "company");
			String address = jsonValueUtil.getValue(customjsArr, "address");//
			String qq = jsonValueUtil.getValue(customjsArr, "qq");//
			String wechat = jsonValueUtil.getValue(customjsArr, "wechat");
			String email = jsonValueUtil.getValue(customjsArr, "email");//
			String mobile_phone = jsonValueUtil.getValue(customjsArr,
					"mobile_phone");//
			String phone = jsonValueUtil.getValue(customjsArr, "phone");//
			String sexstr = jsonValueUtil.getValue(customjsArr, "sex");
			int sex = 0;
			if (!sexstr.isEmpty()) {
				sex = Integer.parseInt(sexstr);
			}
			int age = 0;
			String agestr = jsonValueUtil.getValue(customjsArr, "age");
			if (!agestr.isEmpty()) {
				age = Integer.parseInt(agestr);
			}
			String birthday = jsonValueUtil.getValue(customjsArr, "birthday");
			String loanid = jsonValueUtil.getValue(customjsArr, "loanid");;
			/*String password = jsonValueUtil.getValue(customjsArr, "password");;
			String ran = RandomUtils.getRandomString();*/
			int level = 0;
			String levelstr = jsonValueUtil.getValue(customjsArr, "level");
			if (!levelstr.isEmpty()) {
				level = Integer.parseInt(levelstr);
			}
			
			List<User> users = userImpl.selectByEmployee(employee);
			if (users.size() > 0) {
				User user = users.get(0);
				user.setAddress(address);
				user.setAge(age);
				user.setBirthday(birthday);
				user.setCompany(company);
				user.setDepartment(department);
				user.setEmail(email);
				user.setLevel(level);
				user.setLoanid(loanid);
				user.setMobile_phone(mobile_phone);
				user.setPhone(phone);
				user.setQq(qq);
				user.setSex(sex);
				user.setUsername(username);
				user.setUtil(util);
				user.setWechat(wechat);
				
				int flag = userImpl.updateOne(user);
				
				if (flag == 1) {
					globalMap.put(STATUS_CODE, SUCCESS);
				}
				else {
					globalMap.put(STATUS_CODE, UPDATE_WR);
				}
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
}
